package com.java110.mall.shop.bmo.shopVipLevel.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.coupon.CouponConfDto;
import com.java110.intf.shop.ICouponConfInnerServiceSMO;
import com.java110.intf.shop.IShopVipLevelInnerServiceSMO;
import com.java110.po.coupon.CouponConfPo;
import com.java110.po.shop.ShopVipLevelPo;
import com.java110.mall.shop.bmo.shopVipLevel.IUpdateShopVipLevelBMO;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("updateShopVipLevelBMOImpl")
public class UpdateShopVipLevelBMOImpl implements IUpdateShopVipLevelBMO {

    @Autowired
    private IShopVipLevelInnerServiceSMO shopVipLevelInnerServiceSMOImpl;
    @Autowired
    private ICouponConfInnerServiceSMO couponConfInnerServiceSMOImpl;

    /**
     * @param shopVipLevelPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> update(ShopVipLevelPo shopVipLevelPo, JSONObject reqJson) {

        int flag = shopVipLevelInnerServiceSMOImpl.updateShopVipLevel(shopVipLevelPo);

        if (flag > 0) {

            CouponConfDto couponConfDto = new CouponConfDto();
            couponConfDto.setShopId(shopVipLevelPo.getShopId());
            couponConfDto.setVipLevelId(shopVipLevelPo.getVipLevelId());
            List<CouponConfDto> couponConfDtos = couponConfInnerServiceSMOImpl.queryCouponConfs(couponConfDto);
            if (couponConfDtos != null && couponConfDtos.size() > 0) {
                for (CouponConfDto couponConf : couponConfDtos) {
                    CouponConfPo couponConfPo = new CouponConfPo();
                    couponConfPo.setConfId(couponConf.getConfId());
                    flag = couponConfInnerServiceSMOImpl.deleteCouponConf(couponConfPo);
                    if (flag < 1) {
                        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "删除关系失败失败");
                    }
                }
            }
            JSONArray couponValues = reqJson.getJSONArray("couponValues");
            if (couponValues.size() < 1 || couponValues == null) {
                return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
            }
            for (int couponConfIndex = 0; couponConfIndex < couponValues.size(); couponConfIndex++) {
                CouponConfPo couponConfPo = BeanConvertUtil.covertBean(couponValues.get(couponConfIndex), CouponConfPo.class);
                couponConfPo.setConfId(GenerateCodeFactory.getGeneratorId("11"));
                couponConfPo.setVipLevelId(shopVipLevelPo.getVipLevelId());
                flag = couponConfInnerServiceSMOImpl.saveCouponConf(couponConfPo);
                if (flag < 1) {
                    return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
                }
            }
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }

}
